package com.example.question.list.list1;

/**
 * @Date 2023-07-02
 * @Author Jonathan
 * @link { <a href="https://leetcode.cn/problems/remove-linked-list-elements/">...</a> }
 */
public class Code01_203 {
    public static void main(String[] args) {
        // 1,2,6,3,4,5,6
        int[] nums = new int[]{3, 1, 2, 5, 6, 4};
        ListNode head = List.createList(nums);
        Code01_203 code = new Code01_203();
        ListNode ret = code.removeElements(head, 6);
        System.out.println(ret);

    }

    /**
     * 添加虚拟头结点
     */
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(0, head);
        ListNode pre = dummyHead;
        ListNode cur = dummyHead.next;
        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
            } else {
                pre = pre.next;

            }
            cur = cur.next;
        }
        return dummyHead.next;
    }

}
